﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PluginInterface;
using System.IO;
using System.Reflection;

namespace Pancernik_WPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            LoadPlugins();

            
        }

        private UIElement control = null;

        private void LoadPlugins()
        {
            
            foreach (string fileOn in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"Plugins"))
            {
                FileInfo file = new FileInfo(fileOn);

                //Preliminary check, must be .dll
                if (file.Extension.Equals(".dll"))
                {
                    //Add the 'plugin'
                    // this.AddPlugin(fileOn);
                    listBox1.Items.Add(fileOn);
                }
            }
            
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                Assembly pluginAssembly = Assembly.LoadFrom(listBox1.SelectedItem as string);


                foreach (Type pluginType in pluginAssembly.GetTypes())
                {
                    if (pluginType.IsPublic) //Only look at public types
                    {
                        if (!pluginType.IsAbstract)  //Only look at non-abstract types
                        {
                            //Gets a type object of the interface we need the plugins to match
                            Type typeInterface = pluginType.GetInterface("PluginInterface.IPluign", true);

                            //Make sure the interface we want to use actually exists
                            if (typeInterface != null)
                            {
                                //Create a new available plugin since the type implements the IPlugin interface
                                //Types.AvailablePlugin newPlugin = new Types.AvailablePlugin();

                                //Set the filename where we found it
                                //newPlugin.AssemblyPath = FileName;

                                //Create a new instance and store the instance in the collection for later use
                                //We could change this later on to not load an instance.. we have 2 options
                                //1- Make one instance, and use it whenever we need it.. it's always there
                                //2- Don't make an instance, and instead make an instance whenever we use it, then close it
                                //For now we'll just make an instance of all the plugins

                                IPluign plugin;
                                
                                plugin = (IPluign)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));


                                label1.Content = plugin.Name;
                                label2.Content = plugin.Author;

                                if (control != null)
                                {
                                    grid1.Children.Remove(control);
                                }
                                
    
                                grid1.Children.Add(control = plugin.MainForm);

                                //Set the Plugin's host to this class which inherited IPluginHost
                                //newPlugin.Instance.Host = this;

                                //Call the initialization sub of the plugin
                                //newPlugin.Instance.Initialize();

                                //Add the new plugin to our collection here
                                //this.colAvailablePlugins.Add(newPlugin);

                                //cleanup a bit
                                //newPlugin = null;
                            }

                            typeInterface = null; //Mr. Clean			
                        }
                    }
                }
            }
        }
    }
}
